$(function () {
    findBusiness();
})

var  newsModel={
    "newsCategory.id":"",
    page:1,
    limit:6
};



// 根据传入的分类id,页码和每页条数查询业务。
function findBusiness() {
    layui.use(['laypage', 'layer'], function() {
        var laypage = layui.laypage
            , layer = layui.layer;
        var businessRes;


        let bcid = sessionStorage.getItem("newsCategoryid");
        if (bcid != null){
            newsModel["newsCategory.id"] = bcid;  // 获取传入的分类id
        }
        businessRes=myAjax(ipAddress()+"/news/g",newsModel, "get",false);

        let businessCateData=myAjax(ipAddress()+"/news/category/g",{},"get",false);

        setBusinessCategory(businessCateData.data);
        setLocationNow(businessCateData.data);
        laypage.render({
            elem: 'page'
            ,count: businessRes.count //数据总数
            ,limit: 9
            ,jump: function(obj){ //obj中obj.curr是当前页码,obj.limit是每一页包含的数据数目.
                newsModel.page=obj.curr;
                newsModel.limit=obj.limit;
                let res = myAjax(ipAddress()+"/news/g", newsModel, "get",false);
                if (res != null) {
                    setBusiness(res.data);
                }
            }
        });
    });
}
// 设置业务范围右边的导航栏
function setBusinessCategory(data) {
    let html='<div>新闻中心</div>';
    for(let i=0;i<data.length;i++){
        html+='<div onclick="findbyBusinessCategory('+data[i].id+')">'+data[i].name+'</div>';
    }
html+='<div><img src="../img/zs_2.jpg"></div>';
    $("#right-navigate").html(html);
}


// 点击分类后传入id并查询数据
function findbyBusinessCategory(bcId){
        sessionStorage.setItem("newsCategoryid",bcId);
        newsModel["newsCategory.id"]=bcId;
        findBusiness();
}

//点击分类后增加当前位置的小导航
function setLocationNow(data) {
    let html='您的当前位置 <a href="/html/index.html">网站首页</a> ≡ <a onclick="navgate()">新闻中心</a> ';
    // let id=sessionStorage.getItem("businessCategoryId");
    let id=newsModel["newsCategory.id"];
    if(id==undefined || id == null){
        html+='';
    }else{
        for(let i=0;i<data.length;i++){
            if(data[i].id==id){
                html+='<a style="cursor: pointer;" onclick="findbyBusinessCategory('+data[i].id+')">  ≡ '+data[i].name+'</a>';
            }
        }
    }

    $("#location").html(html);
}
function navgate() {
    sessionStorage.removeItem("newsCategoryid");
    window.location.href="/html/news.html"

}


// 设置业务数据
function setBusiness(data){
        let html='';
        for(let i=0;i<data.length;i++) {
            html += '<div>\n' +
                '                    <div style="cursor: pointer" onclick="findBusinessDetail('+data[i].id+')"><img src="'+data[i].imgHref+'"/></div>\n' +
                '                    <div class="news-font">\n' +
                '                        <div class="ddd"  onclick="findBusinessDetail('+data[i].id+ ')">'+data[i].name+'</div>\n' +
                '                        <div>'+data[i].createTime+'<br>'+data[i].content+'</div>\n' +
                '                    </div>\n' +
                '                </div> ';
        }
    $("#news").html(html);
}


// 根据id查询具体的业务
function findBusinessDetail(id) {
    sessionStorage.setItem("newsDId",id);
    window.location.href="/html/news-detail.html";
}